var express = require('express');
var router = express.Router();
const mysql = require('mysql');//导入数据库模块
const createDB = require('../db/dbOp');//导入数据库操作模块

/* GET home page. */
router.get('/', function (req, res, next) {
  res.render('index', { title: 'Express' });
});

router.post('/login', function (req, res, next) {
  var obj = {};//返回前端的数据
  var Data = req.body;//接受前端数据
  const conn = mysql.createConnection({
    host: "localhost",         //主机地址：127.0.01
    database: "xst",     //数据库名称  
    user: "root",             //数据库账户名
    password: "root"           //数据库密码
  });
  //2.建立连接
  conn.connect();
  //let sql = "select * from xst.user"; username,xuehao,idcard FROM `user`
  console.log(Data, typeof (Data));

  let sql = "SELECT * from xst.user WHERE username='" + Data.name + "' AND xuehao='" + Data.idxuehao + "'";
  createDB(sql).then(data => {
    //console.log(Data);
    //data=JSON.stringify(data)
    //console.log(data)
    if (data[0] == '') {
      obj = { code: "501", msg: "数据库查询失败" };
      res.send(JSON.stringify(obj));//将结果响应给前端
    } else {
      console.log(data)
      obj = { code: '200', msg: "数据库查询成功！", body: data[0].idcard };
      res.send(JSON.stringify(obj))
    }

  }, err => {
    obj = { code: "501", msg: "数据库查询失败" };
    res.send(JSON.stringify(obj));//将结果响应给前端
  });
  conn.end();//关闭数据库连接
});
router.post('/regist', function (req, res, next) {
  var obj = {};  //返回前端的数据
  var app = req.body;  //接受前端数据
  const conn = mysql.createConnection({
    host: "localhost",         //主机地址：127.0.01
    database: "xst",            //数据库名称  
    user: "root",             //数据库账户名
    password: "root"           //数据库密码
  });
  //2.建立连接
  conn.connect();
  console.log(app.xh);
  let sql = "SELECT xuehao FROM `user` WHERE xuehao='" + app.xh + "'";
  createDB(sql).then(data => {

    if (data == '') {
      //连接数据库
      const conn = mysql.createConnection({
        host: "localhost",         //主机地址：127.0.01
        database: "xst",            //数据库名称  
        user: "root",             //数据库账户名
        password: "root"           //数据库密码
      });
      conn.connect();
      // 数据库插入数据                                                                      
      let sql = `insert into user (username,password,xuehao,xueyuan,zhuanye,idcard,banji) values (" ${app.name} "," ${app.pw} "," ${app.xh} "," ${app.xy} "," ${app.zy} "," ${app.id} "," ${app.bj} ")`;
      createDB(sql).then(data => {
        console.log('下一步，数据库插入数据000000000000')
        console.log(data)
        // 成功插入并返回信息
        obj = { code: '201', msg: "注册成功" };
        res.send(JSON.stringify(obj));
      }, err => {
        console.log('if 的：', err)
        obj = { code: "501", msg: "注册失败" };
        res.send(JSON.stringify(obj));//将结果响应给前端
      });
    }
    else {
      obj = { code: '200', msg: "用户名已存在" };
      res.send(JSON.stringify(obj));
    }

  }, err => {
    console.log('最后的：', err)
    obj = { code: "502", msg: "注册失败" };
    res.send(JSON.stringify(obj));//将结果响应给前端
  });

  conn.end();//关闭数据库连接
});
router.post('/stu', function (req, res, next) {
  var obj = {};//返回前端的数据
  var data = req.body;//接受前端数据
  const conn = mysql.createConnection({
    host: "localhost",         //主机地址：127.0.01
    database: "xst",     //数据库名称  
    user: "root",             //数据库账户名
    password: "root"           //数据库密码
  });
  //2.建立连接
  conn.connect();
  //let sql = "select * from xst.user";
  console.log(data, typeof (data));
})

router.post('/stujz', function (req, res, next) {
  var obj = {};  //返回前端的数据
  var data = req.body;  //接受前端数据
  const conn = mysql.createConnection({
  });
  //2.建立连接
  conn.connect();
})
module.exports = router;
